//
//  DHSearchCategoryCell.m
//  Dresshere
//
//  Created by ZhangPeng on 2018/2/10.
//  Copyright © 2018年 张朋. All rights reserved.
//

#import "DHSearchCategoryCell.h"
#import "DHSearchCategoryItemView.h"

@interface DHSearchCategoryCell()

@property (strong, nonatomic) UIScrollView *scrollView;

@property (strong, nonatomic) NSArray *dataArr;

@end

@implementation DHSearchCategoryCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        if (!self.scrollView) {
            self.scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, 54)];
            [self.contentView addSubview:self.scrollView];
        }
        UIView *line = [[UIView alloc] initWithFrame:CGRectMake(0, 53, kScreenWidth, 1)];
        line.backgroundColor = [UIColor colorWithHexString:@"#ECF2F1"];
        [self.contentView addSubview:line];
    }
    return self;
}

- (NSArray *)dataArr{
    if (!_dataArr) {
        _dataArr = [NSArray array];
    }
    return _dataArr;
}

- (void)layoutSubviews{
    self.scrollView.frame = CGRectMake(0, 0, self.width, self.height);
}

- (void)updateCellWithConditionArr:(NSArray *)conditionArr firstLoad:(BOOL)firstLoad{
    self.dataArr = conditionArr;
    if (firstLoad) {
        [self.scrollView removeAllSubviews];
        __block CGFloat spaceX = 5;
        __block CGFloat logoW = 50.5;
        __block CGFloat itemLeft = spaceX;
        __weak __typeof(self)weakSelf = self;
        [conditionArr enumerateObjectsUsingBlock:^(DHCategoryModel *category, NSUInteger idx, BOOL * _Nonnull stop) {
            CGFloat itemW = logoW + [category.title widthForFont:[UIFont systemFontOfSize:14]];
            DHSearchCategoryItemView *categoryItemView = [[DHSearchCategoryItemView alloc] initWithFrame:CGRectMake(itemLeft, 0, itemW, weakSelf.height) categoryTitle:category.title categoryImgURLString:category.icon];
            [weakSelf.scrollView addSubview:categoryItemView];
            categoryItemView.tag = idx;
            [categoryItemView addTarget:self action:@selector(clickAction:) forControlEvents:UIControlEventTouchUpInside];
            itemLeft += (itemW + spaceX);
        }];
        self.scrollView.contentSize = CGSizeMake(itemLeft, 0);
    }
}

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)clickAction:(UIControl *)sender{
    for (DHSearchCategoryItemView *item in self.scrollView.subviews) {
        if ([item isKindOfClass:[DHSearchCategoryItemView class]]) {
            if (item.tag != sender.tag) {
                item.selected = YES;
                [item clickAction:item];
            }
        }
    }
    DHCategoryModel *model = self.dataArr[sender.tag];
    if (sender.isSelected) {
        [self routerEventWithName:@"DHSearchCategoryCell" userInfo:@{@"category_id":model._id}];
    }else{
        [self routerEventWithName:@"DHSearchCategoryCell" userInfo:@{@"category_id":[NSNull null]}];
    }
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
